package com.grasscove.namethat;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.grasscove.namethat.adapters.CategoriesListAdapter;
import com.grasscove.namethat.quickaction.ActionItem;
import com.grasscove.namethat.quickaction.QuickAction;

public class CategoriesList extends ListActivity {
	public static final String[] CATEGORIES = {
		"Everything", "Inventions",
		"History", "Entertainment",
		"Sports", "Geography",
		"Everyday", "Consumption",
		"Miscellaneous", "Animals"
	};

	public static final int EDIT_WORDLIST = 0;
	public static final int DELETE_CATEGORY = 1;

	private ActionItem mPlayActionItem = null;
	private ActionItem mEditActionItem = null;
	private QuickAction mQuickAction = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.categories);

		mPlayActionItem = new ActionItem();
		mPlayActionItem.setTitle("Play");
		mPlayActionItem.setIcon(getResources().getDrawable(R.drawable.start_icon));
		mPlayActionItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CategoriesList.this, Play.class);
				intent.putExtra("category", mQuickAction.position);
				startActivity(intent);
				//Toast.makeText(CategoriesList.this, "Play selected", Toast.LENGTH_SHORT).show();
			}
		});

		mEditActionItem = new ActionItem();
		mEditActionItem.setTitle("Edit Word List");
		mEditActionItem.setIcon(getResources().getDrawable(R.drawable.edit_icon));
		mEditActionItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CategoriesList.this, WordsList.class);
        		intent.putExtra("category", mQuickAction.position);
        		startActivity(intent);
			}
		});

		setListAdapter(new CategoriesListAdapter(this, CATEGORIES));
		registerForContextMenu(getListView());
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		mQuickAction = new QuickAction(v, position);

		mQuickAction.addActionItem(mPlayActionItem);
		mQuickAction.addActionItem(mEditActionItem);
		mQuickAction.setAnimStyle(QuickAction.ANIM_AUTO);

		mQuickAction.show();
	}

	/*
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

        mSelectedCategory = info.position;

        menu.setHeaderTitle(CATEGORIES[info.position]);
        menu.add(0, EDIT_WORDLIST, 0, "Edit Wordlist");
        //menu.add(0, DELETE_CATEGORY, 2, "Delete Category");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()) {
            case EDIT_WORDLIST:
            	Intent intent = new Intent(this, WordsList.class);
        		intent.putExtra("category", mSelectedCategory);
        		startActivity(intent);
                return true;
            case DELETE_CATEGORY:
                //showDialog(DIALOG_CONFIRM_DELETE);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
	}
	*/

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, HomeActivity.OPTION_MENU_PLAY, 1, "Play").setIcon(R.drawable.play_menu_icon);
		menu.add(0, HomeActivity.OPTION_MENU_RULES, 2, "Rules").setIcon(R.drawable.rules_menu_icon);
		//menu.add(0, HomeActivity.OPTION_MENU_ABOUT, 3, "About").setIcon(R.drawable.play_menu_icon);
		menu.add(0, HomeActivity.OPTION_MENU_SETTINGS, 4, "Settings").setIcon(R.drawable.settings_menu_icon);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent mIntent;
		switch (item.getItemId()) {
			case HomeActivity.OPTION_MENU_PLAY:
				mIntent = new Intent(this, CategoriesList.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			case HomeActivity.OPTION_MENU_RULES:
				mIntent = new Intent(this, Rules.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			case HomeActivity.OPTION_MENU_ABOUT:
				break;
			case HomeActivity.OPTION_MENU_SETTINGS:
				mIntent = new Intent(this, SettingsList.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			default:
				break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		if(mQuickAction != null) {
			mQuickAction.dismiss();
		}
	}
}